<template>
  <div class="page-content">
    <cy-table
      :dataList="dataList"
      :listCount="listCount"
      :contentConfig="contentConfig"
      v-model:page="pageInfo"
    >
      <template #handler>
        <el-button
          v-if="isCreate"
          type="success"
          icon="el-icon-circle-plus"
          circle
          @click="handleNewClick"
        ></el-button>
        <el-button
          type="warning"
          icon="el-icon-refresh"
          circle
          @click="handleRefreshClick()"
        ></el-button>
      </template>
      <template #createAt="scope">
        <strong>{{ $filters.formatTime(scope.row.createAt) }}</strong>
      </template>
      <template #updateAt="scope">
        <strong>{{ $filters.formatTime(scope.row.updataAt) }}</strong>
      </template>
      <template #handle="scope">
        <el-button
          v-if="isDelete"
          type="text"
          size="mini"
          icon="el-icon-delete"
          @click="handleDelClick(scope.row)"
          >删除</el-button
        >
        <el-button
          v-if="isUpdate"
          type="text"
          size="mini"
          icon="el-icon-edit"
          @click="handleEditClick(scope.row)"
          >编辑</el-button
        >
      </template>

      <!-- 在page-content中动态插入剩余的插槽 -->
      <template
        v-for="item in otherPropSlots"
        :key="item.prop"
        #[item.slotName]="scope"
      >
        <template v-if="item.slotName">
          <slot :name="item.slotName" :row="scope.row"></slot>
        </template>
      </template>
    </cy-table>
  </div>
</template>

<script lang="ts">
import { defineComponent, computed, ref, watch } from 'vue'
import { useVuexState } from '@/store'
import CyTable from '@/base-ui/table'
import usePermissions from '@/hooks/usePermissions'

export default defineComponent({
  props: {
    contentConfig: {
      type: Object,
      required: true
    },
    pageName: {
      type: String,
      required: true
    }
  },
  components: {
    CyTable
  },
  emits: ['newBtnClick', 'editBtnClick'],
  setup(props, { emit }) {
    const store = useVuexState()

    // 0.获取该用户权限
    const isCreate = usePermissions(props.pageName, 'create')
    const isDelete = usePermissions(props.pageName, 'delete')
    const isUpdate = usePermissions(props.pageName, 'update')
    const isQuery = usePermissions(props.pageName, 'query')

    // 1.双向绑定pageInfo
    const pageInfo = ref({
      pageSize: 10,
      currentPage: 1
    })
    watch(pageInfo, () => getPageList(), {
      deep: true
    })

    // 2.向vuex发送请求
    const getPageList = (queryInfo: any = {}) => {
      store.dispatch('system/getPageListAction', {
        pageName: props.pageName,
        queryInfo: {
          offset: (pageInfo.value.currentPage - 1) * pageInfo.value.pageSize,
          size: pageInfo.value.pageSize,
          ...queryInfo
        }
      })
    }
    getPageList()

    // 3.拿到vuex里的数据
    const dataList = computed(() =>
      store.getters[`system/pageDataList`](props.pageName)
    )

    const listCount = computed(() =>
      store.getters[`system/pageDataCount`](props.pageName)
    )

    // 4.获取其他的动态插槽名称
    const otherPropSlots = props.contentConfig?.propList.filter((item: any) => {
      if (item.slotName === 'handle') return false
      if (item.slotName === 'createAt') return false
      if (item.slotName === 'updateAt') return false
      return true
    })

    // 5.向vuex发送 删除/新建/编辑 请求
    const handleDelClick = (item: any) => {
      store.dispatch('system/delPageDataAction', {
        pageName: props.pageName,
        id: item.id
      })
    }
    const handleNewClick = () => {
      emit('newBtnClick')
    }
    const handleEditClick = (item: any) => {
      emit('editBtnClick', { ...item })
    }
    const handleRefreshClick = () => {
      pageInfo.value.currentPage = 1
      getPageList()
    }

    const dialogTitle = ref('')

    return {
      dataList,
      listCount,
      pageInfo,
      getPageList,
      otherPropSlots,
      isCreate,
      isDelete,
      isUpdate,
      isQuery,
      handleDelClick,
      handleNewClick,
      handleEditClick,
      handleRefreshClick,
      dialogTitle
    }
  }
})
</script>

<style scoped lang="less">
.page-content {
  padding: 20px;
  margin-top: 20px;
  background-color: #fff;
}
</style>
